package com.inwavetech.os.entity;

import com.inwavetech.os.R;
import com.inwavetech.os.util.DefaultDateFormatter;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by jgskin on 2/7/14.
 */
@DatabaseTable(tableName = "os")
public class Os {

    public static final HashMap<Enum, Integer> statusResources;

    public static final HashMap<Enum, Integer> closingStatusResources;

    @DatabaseField(generatedId = true)
    public int id;

    @DatabaseField(canBeNull = false)
    public String original_id;

    @DatabaseField()
    public String client;

    @DatabaseField()
    public String store;

    @DatabaseField()
    public String opener;

    @DatabaseField()
    public String origin;

    @DatabaseField()
    public String contact;

    @DatabaseField()
    public String authorization;

    @DatabaseField()
    public Date activated_date;

    @DatabaseField()
    public String activated_user;

    @DatabaseField()
    public String finished_user;

    @DatabaseField()
    public String assignment;

    @DatabaseField()
    public Date assignment_date;

    @DatabaseField()
    public String assignment_time;

    @DatabaseField()
    public String maintenance_observation;

    @DatabaseField()
    public Date created_at;

    @DatabaseField()
    public Date finished_at;

    @DatabaseField(canBeNull = true)
    public Date updated_at;

    @ForeignCollectionField()
    public ForeignCollection<Product> products;

    @DatabaseField(foreign = true, canBeNull = false)
    public User user;

    @DatabaseField(canBeNull = false)
    public Status status;

    @DatabaseField(canBeNull = false)
    public ServiceOrderType service_order_type;

    /* finalized os fields */

    @DatabaseField()
    public String service_justification;

    @DatabaseField()
    public String travel_justification;

    @DatabaseField()
    public String roundup;

    @DatabaseField()
    public String future_observation;

    @DatabaseField()
    public String name_responsible;

    @DatabaseField()
    public BigDecimal service_price;

    @DatabaseField()
    public BigDecimal travel_price;

    @DatabaseField()
    public ClosingStatus closing_status;

    @DatabaseField()
    public String signature_filename;

    @DatabaseField()
    public boolean send_to_server = false;

    /* finalized os fields */

    public enum Status {
        OPEN, ACTIVE, FINISHED;

    }
    public enum ClosingStatus {
        CONCLUDED, ON_HOLD, CANCELED;

    }


    public enum ServiceOrderType {
        INSTALL, MAINTENANCE;
    }
    private static final HashMap<Enum, Integer> serviceOrderTypeResources;

    static {
        // map the os status to string resources
        statusResources = new HashMap<Enum, Integer>();
        statusResources.put(Status.OPEN, R.string.os_status_open);
        statusResources.put(Status.ACTIVE, R.string.os_status_active);
        statusResources.put(Status.FINISHED, R.string.os_status_finished);

        // map the os closing status to string resources
        closingStatusResources = new HashMap<Enum, Integer>();
        closingStatusResources.put(ClosingStatus.CONCLUDED, R.string.os_closing_status_concluded);
        closingStatusResources.put(ClosingStatus.ON_HOLD, R.string.os_closing_status_onhold);
        closingStatusResources.put(ClosingStatus.CANCELED, R.string.os_closing_status_canceled);

        // map the os closing status to string resources
        serviceOrderTypeResources = new HashMap<Enum, Integer>();
        serviceOrderTypeResources.put(ServiceOrderType.INSTALL, R.string.os_service_order_type_install);
        serviceOrderTypeResources.put(ServiceOrderType.MAINTENANCE, R.string.os_service_order_type_maintenance);
    }

    public int getStatusResource() {
        return statusResources.get(status);
    }

    public int getClosingStatusResource() {
        return closingStatusResources.get(closing_status);
    }

    public int getServiceOrderTypeResource() {
        return serviceOrderTypeResources.get(service_order_type);
    }

    public String getStringCreatedDate() {
        return DefaultDateFormatter.format(this.created_at);
    }

    public String getStringActivatedDate() {
        return DefaultDateFormatter.format(this.activated_date);
    }

    public String getStringAssignmentDate() {
        return DefaultDateFormatter.format(this.assignment_date);
    }

    public boolean isOwner(User user) {
        return user.username.equals(user.username);
    }
}
